package com.yinzifan.manager.error;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.ErrorAttributes;

public class MyErrorController extends BasicErrorController{

	public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties,
			List<ErrorViewResolver> errorViewResolvers) {
		super(errorAttributes, errorProperties, errorViewResolvers);
	}

	@Override
	protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
		Map<String, Object> errorAttributes = super.getErrorAttributes(request, includeStackTrace);
		/**
		 * {
			    "timestamp": "2018-07-30 23:02:59",
			    "status": 500,
			    "error": "Internal Server Error",
			    "message": "No message available",
			    "path": "/manager/products/"
			}
		 */
		errorAttributes.remove("timestamp");
		errorAttributes.remove("error");
		errorAttributes.remove("status");
		errorAttributes.remove("path");
		errorAttributes.remove("exception");
		String errorCode = (String)errorAttributes.get("message");
		ErrorEnum errorEnum = ErrorEnum.getByCode(errorCode);
		errorAttributes.put("message", errorEnum.getMessage());
		errorAttributes.put("code", errorEnum.getCode());
		errorAttributes.put("canRetry", errorEnum.isCanRetry());
		return errorAttributes;
	}
	
	
	
}
